/**
 * 导出配置 弹窗
 */

import { Checkbox, message, Modal } from "antd";
import { FC, useCallback, useEffect, useState } from "react";
import { getGbShipFormGetExportExcel, getGbShipFormSetExportExcel, IGbShipFormGetExportExcel } from "../../api";
import { CheckboxChangeEvent } from "antd/es/checkbox";

const DaochuSetModule: FC<{
    isModalOpen: boolean;
    daochuSetModuleCallback: () => void;
}> = ({ isModalOpen, daochuSetModuleCallback }) => {
    const [listData, setListData] = useState<IGbShipFormGetExportExcel[]>([]);
    const listFun = useCallback(
        async () => {
            const res = await getGbShipFormGetExportExcel() as { code: number, msg: string, data: IGbShipFormGetExportExcel[] };
            if (res.code === 20000) {
                setListData(res.data || []);
            } else {
                message.error(res.msg || '获取导出配置失败');
            }
        },
        [],
    )
    useEffect(() => {
        listFun()

        return () => {

        }
    }, [listFun])

    const onChange = (e: CheckboxChangeEvent) => {
        const { checked, value } = e.target;
        setListData((prev) => prev.map((item) => item.value === value ? { ...item, isSel: checked ? 1 : 0 } : item));
    }
    const handleOk = async () => {
        let exportExcel = listData.filter((item) => item.isSel === 1).map((item) => item.value).join(',');
        const res =await getGbShipFormSetExportExcel({exportExcel}) as { code: number, msg: string };
        if(res.code === 20000) {
            message.success('导出配置成功');
            daochuSetModuleCallback();
        } else {
            message.error(res.msg || '导出配置失败');
        }
    }
    const handleCancel = () => {
        console.log('取消配置')
        daochuSetModuleCallback();
    }


    return <Modal
        title="导出设置"
        closable={{ 'aria-label': 'Custom Close Button' }}
        open={isModalOpen}
        onOk={handleOk}
        onCancel={handleCancel}
    >
        {
            listData.map((item) => (<Checkbox onChange={onChange} checked={item.isSel === 1} value={item.value}>{item.name}</Checkbox>))
        }
    </Modal>
}

export default DaochuSetModule;